package main

import (
	"bluebell/controller"
	"bluebell/dao/mysql"
	"bluebell/dao/redis"
	"bluebell/logger"
	"bluebell/pkg/snowflake"
	"bluebell/router"
	"bluebell/settings"
	"fmt"
	"strconv"
)

// @title 标题
// @version 1.0
// @description 这里写描述信息
// @termsOfService http://swagger.io/terms/

// @contact.name 联系人
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host 127.0.0.1:8985
// @BasePath /api
func main() {
	// 1. 加载配置
	if err := settings.Init(); err != nil {
		fmt.Printf("init config failed, err:%v\n", err)
		return
	}
	// 2. 初始化日志
	if err := logger.Init(settings.Conf.LogConfig, settings.Conf.Mode); err != nil {
		fmt.Printf("init logger failed, err:%v\n", err)
		return
	}
	// 3. 初始化MySQL
	if err := mysql.Init(settings.Conf.MySQLConfig); err != nil {
		fmt.Printf("init mysql failed, err:%v\n", err)
		return
	}
	defer mysql.Close()
	// 4. 初始化Redis连接
	if err := redis.Init(settings.Conf.RedisConfig); err != nil {
		fmt.Printf("init redis failed, err:%v\n", err)
		return
	}
	defer redis.Close()
	// 5. 初始化雪花算法
	if err := snowflake.Init(settings.Conf.SnowFlake.StartTime, settings.Conf.SnowFlake.MachineID); err != nil {
		fmt.Printf("init redis failed, err:%v\n", err)
		return
	}
	// 6.初始化翻译器(使用gin框架内置的校验器)
	if err := controller.InitTrans("zh"); err != nil {
		fmt.Printf("init translate failed, err:%v\n", err)
	}
	// 7. 注册路由
	r := router.SetupRouter()
	// 8. 启动服务
	err := r.Run(":" + strconv.Itoa(settings.Conf.Port))
	if err != nil {
		fmt.Printf("app running failed, err:%v\n", err)
	}
}
